package com.comtrade.map;


import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;

import com.example.beaconapk.R;

public class DotView extends View{
	
	private float xCoor=0, yCoor=0;
	private int h,w;
	
	private float oldX, oldY;
	private long animationDuration;
	
	private View parentView;
	
	private Drawable logo;
	
	public DotView(Context context, View parentView){
		this(context);
		this.logo = getResources().getDrawable(R.drawable.ct_logo);
		this.logo.setBounds(0, 0, 30, 30);
		this.animationDuration = 500;
		//this.setCoordinates(0, 0);
		this.parentView = parentView;
		
		
	}
	//inicijalizacija za matrix;
	Matrix matrix=new Matrix();
	
	public DotView(Context context){
        super(context);
	}
	/**
	 * draws current position
	 */
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		
		
		
		//matrix.reset();
		canvas.save();
		
		canvas.concat(matrix);
		
		canvas.translate(getW()/2, getH()/2);
		//stavio sa proizvolno za scail posto nzm sta tu treba da ide
		//canvas.scale(50,50);
		//canvas.translate(-getW()/2, -getH()/2);
		canvas.translate(xCoor,yCoor);
		logo.draw(canvas);
		canvas.restore();
	}
	
	public int getH() {
		return h;
	}
	
	public void setH(int h) {
		this.h = h;
	}
	
	public int getW() {
		return w;
	}
	
	public void setW(int w) {
		this.w = w;
	}
	
	public void setAnimationDuration(long d) {
		animationDuration = d;
	}
	
	
	
	public float getxCoor() {
		return xCoor;
	}

	public void setxCoor(float xCoor) {
		this.xCoor = xCoor;
	}

	public float getyCoor() {
		return yCoor;
	}

	public void setyCoor(float yCoor) {
		this.yCoor = yCoor;
	}
	
	/**
	 * animating method which lasts for animationDuration
	 * from current position to (x,y). 
	 * @param x
	 * @param y
	 */
	public void setCoordinates(final float x, final float y){
		ValueAnimator va = ValueAnimator.ofFloat(0, 1);
		va.setDuration(animationDuration);
		oldX = xCoor;
		oldY = yCoor;
		va.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				float value = animation.getAnimatedFraction();
				float dx = x - oldX;
				float dy = y - oldY;
				xCoor = oldX + dx * value;
				yCoor = oldY + dy * value;	
				Log.d("AAAAA", " "+parentView.toString());
				parentView.invalidate();
			}
		});
		va.start();
	}
	@Override
	public String toString() {
		return "DotView [xCoor=" + xCoor + ", yCoor=" + yCoor + ", h=" + h
				+ ", w=" + w + ", oldX=" + oldX + ", oldY=" + oldY
				+ ", animationDuration=" + animationDuration + ", parentView="
				+ parentView + ", logo=" + logo + ", matrix=" + matrix + "]";
	}
	
}
